package com.java1412.service.impl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.java1412.entity.User;
import com.java1412.mapper.UserMapper;
import com.java1412.service.UserService;
import com.java1412.utils.ResponseUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author java1412
 * @since 2023-06-20 16:44:55
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    // 注册stringRedisTemplate
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResponseUtil findById(Integer id) {
        User user = userMapper.findById(id);
        return ResponseUtil.ok("查询用户成功", user);
    }

    @Override
    public ResponseUtil findByUsername(User user) {
        ResponseUtil r = null;
        // 查数据库
        User userPo = userMapper.findByUsername(user.getUsername());
        if (userPo == null) {
            // 用户名不存在
            r = ResponseUtil.fail("登录失败,用户名不存在");
        } else if (!user.getPassword().equals(userPo.getPassword())) {
            r = ResponseUtil.fail("登录失败,用户名和密码不匹配");
        } else if (userPo.getRole().intValue() == 0) {
            r = ResponseUtil.fail("登录失败,角色不正确");
        } else {
            userPo.setPassword(null);
            r = ResponseUtil.ok("登录成功", userPo);
        }
        return r;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ResponseUtil register(User user, String uuid, String code) {
        ResponseUtil r = null;
        // 1.从redis中获取手机号和验证，匹配
        String phoneAndCode = stringRedisTemplate.opsForValue().get(uuid);
        if (null == phoneAndCode) {
            r = ResponseUtil.fail("验证码不存在");
        } else {
            // 验证码和手机号在redis中是存在的
            // shfidhfdghidfghidghdifhgidf:18291986656-1234
            String[] pacs = phoneAndCode.split("-");
            String redisPhone = pacs[0];    // 缓存中的手机号
            String redisCode = pacs[1]; // 缓存中的验证码
            if (!redisPhone.equals(user.getPhone())) {
                Map<String, Object> map = new HashMap<>();
                map.put("phone", "手机号不正确");
                r = ResponseUtil.fail("验证不正确");
                r.setData(map);
                return r;
            }
            if (!redisCode.equals(code)) {
                Map<String, Object> map = new HashMap<>();
                map.put("code", "验证码不正确");
                r = ResponseUtil.fail("验证不正确");
                r.setData(map);
                return r;
            }
        }
        // 验证通过的
        // 2.匹配通过,添加新注册的用户，但是这里有个可以完善的地方就需要对新添加的用户名进行唯一性校验
        user.setEmail("110@qq.com");
        user.setQuestion("王总喜欢谁?");
        user.setAnswer("我喜欢林志玲");
        user.setRole(1); // 1表示普通用户
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        Integer result = userMapper.add(user);
        if (result.intValue() > 0) {
            // 需要将当前uuid在redis中要删除
            stringRedisTemplate.delete(uuid);
            r = ResponseUtil.ok("注册用户成功");
        } else {
            r = ResponseUtil.fail("注册用户失败");
        }
        // 3.返回结果
        return r;
    }

    // 后台登录
    @Override
    public ResponseUtil adminFindByUsername(User user) {
        ResponseUtil r = null;
        // 查数据库
        User userPo = userMapper.findByUsername(user.getUsername());
        if (userPo == null) {
            // 用户名不存在
            r = ResponseUtil.fail("登录失败,用户名不存在");
        } else if (!user.getPassword().equals(userPo.getPassword())) {
            r = ResponseUtil.fail("登录失败,用户名和密码不匹配");
        } else if (userPo.getRole().intValue() != 0) {
            r = ResponseUtil.fail("登录失败,角色不正确");
        } else {
            userPo.setPassword(null);
            r = ResponseUtil.ok("登录成功", userPo);
        }
        return r;
    }

    /**
     * 分页查询
     *
     * @param currentPage 第几页
     * @param pageSize
     * @return
     */
    @Override
    public ResponseUtil find(Integer currentPage, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        Integer startIndexNo = (currentPage - 1) * pageSize;
        map.put("startIndexNo", startIndexNo);
        map.put("pageSize", pageSize);
        // 查询用户信息
        List<User> userList = userMapper.find(map);
        // 查询数据量
        Integer total = userMapper.findCount(map);
        Map<String, Object> result = new HashMap<>();
        result.put("userList", userList);
        result.put("total", total);
        return ResponseUtil.ok("查询用户信息成功", result);
    }
}
